// license-header java merge-point
/**
 * This is only generated once! It will never be overwritten.
 * You can (and have to!) safely modify it by hand.
 */
package fr.upstlse.glre.business.domain;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;

import fr.upstlse.glre.business.vo.ActualiteSearchCriteriaVO;

/**
 * @see fr.upstlse.glre.business.domain.Actualite
 */
public class ActualiteDaoImpl
    extends fr.upstlse.glre.business.domain.ActualiteDaoBase
{
	
    /**
     * @see fr.upstlse.glre.business.domain.ActualiteDao#toActualiteVO(fr.upstlse.glre.business.domain.Actualite, fr.upstlse.glre.business.vo.ActualiteVO)
     */
    public void toActualiteVO(
        fr.upstlse.glre.business.domain.Actualite source,
        fr.upstlse.glre.business.vo.ActualiteVO target)
    {
        super.toActualiteVO(source, target);
        	target.setUserVO(getUserDao().toUserVO(source.getUser()));
    }


    /**
     * @see fr.upstlse.glre.business.domain.ActualiteDao#toActualiteVO(fr.upstlse.glre.business.domain.Actualite)
     */
    public fr.upstlse.glre.business.vo.ActualiteVO toActualiteVO(final fr.upstlse.glre.business.domain.Actualite entity)
    {
        return super.toActualiteVO(entity);
    }


    /**
     * Retrieves the entity object that is associated with the specified value object
     * from the object store. If no such entity object exists in the object store,
     * a new, blank entity is created
     */
    private fr.upstlse.glre.business.domain.Actualite loadActualiteFromActualiteVO(fr.upstlse.glre.business.vo.ActualiteVO actualiteVO)
    {
        fr.upstlse.glre.business.domain.Actualite actualite = this.load(actualiteVO.getId());
        if (actualite == null)
        {
            actualite = fr.upstlse.glre.business.domain.Actualite.Factory.newInstance();
        }
        return actualite;
    }

    
    /**
     * @see fr.upstlse.glre.business.domain.ActualiteDao#actualiteVOToEntity(fr.upstlse.glre.business.vo.ActualiteVO)
     */
    public fr.upstlse.glre.business.domain.Actualite actualiteVOToEntity(fr.upstlse.glre.business.vo.ActualiteVO actualiteVO)
    {
        fr.upstlse.glre.business.domain.Actualite entity = this.loadActualiteFromActualiteVO(actualiteVO);
        this.actualiteVOToEntity(actualiteVO, entity, true);
        return entity;
    }


    /**
     * @see fr.upstlse.glre.business.domain.ActualiteDao#actualiteVOToEntity(fr.upstlse.glre.business.vo.ActualiteVO, fr.upstlse.glre.business.domain.Actualite)
     */
    public void actualiteVOToEntity(
        fr.upstlse.glre.business.vo.ActualiteVO source,
        fr.upstlse.glre.business.domain.Actualite target,
        boolean copyIfNull)
    {
        super.actualiteVOToEntity(source, target, copyIfNull);
        if (copyIfNull || source.getUserVO() != null) {
			target.setUser(getUserDao().userVOToEntity(source.getUserVO()));
		}
    }


	@Override
	protected List handleFindByCriteria(
			ActualiteSearchCriteriaVO pActualiteSearchCriteriaVO)
			throws Exception {
		
		Criteria lCriteria = getSession().createCriteria(Actualite.class);
		if(StringUtils.isNotBlank(pActualiteSearchCriteriaVO.getTitre())) {
			lCriteria.add(Restrictions.eq("titre", pActualiteSearchCriteriaVO.getTitre()));
		}
		if(StringUtils.isNotBlank(pActualiteSearchCriteriaVO.getContenu())) {
			lCriteria.add(Restrictions.eq("contenu", pActualiteSearchCriteriaVO.getContenu()));
		}
		if(pActualiteSearchCriteriaVO.getDate() != null) {
			lCriteria.add(Restrictions.eq("date", pActualiteSearchCriteriaVO.getDate()));
		}
		lCriteria.setMaxResults(pActualiteSearchCriteriaVO.getMax());
		
		return lCriteria.list();
	}
    

}